//
//  HomeTableViewController.swift
//  SinaWeibo
//
//  Created by apple on 15/11/8.
//  Copyright © 2015年 apple. All rights reserved.
//

import UIKit
import AFNetworking
import SVProgressHUD

//定义首页标识符
private let  HomeCellId = "HomeCellId"

class HomeTableViewController: BaseTableViewController {
   
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if !userLogin{
            visitorLoginView?.setUIInfo(nil, title: "关注一些人，回这里看看有什么惊喜")
            
            return
        }
        prepareTableView()
        
        loadData()
    }
    
    //MARK: 注册Cell
    private func prepareTableView(){
        //设置下拉刷新
        refreshControl = UIRefreshControl()
        //添加监听方法
        refreshControl?.addTarget(self, action: "loadData", forControlEvents: .ValueChanged)
        
        tableView.registerClass(StatusCell.self, forCellReuseIdentifier: HomeCellId)
        tableView.estimatedRowHeight = 200
        tableView.rowHeight = UITableViewAutomaticDimension
        
        //设置分割线
        tableView.separatorStyle = .None
        
        tableView.tableFooterView = indicatorView
    }
    
    @objc private func loadData(){

        var since_id = 0
        var max_id = 0
        if indicatorView.isAnimating() {
            //上拉加载更多数据
            since_id = 0
            max_id = statuses.last?.id ?? 0
        } else {
            //下拉刷新
            max_id = 0
            since_id = statuses.first?.id ?? 0
        }

        StatusListViewModel.loadData(since_id: since_id, max_id: max_id) { (array) -> () in
            
            self.refreshControl?.endRefreshing()
            
            if array == nil {
                //请求失败
                SVProgressHUD.showErrorWithStatus(AppErrorTip)
                //加载数据失败 停止动画
                self.indicatorView.stopAnimating()
                return
            }
            
            //一定拿到数据  赋值]
            if since_id > 0 {
                //下拉刷新 直接追加数据
                self.statuses = array! + self.statuses
            } else if max_id > 0 {
                //上拉加载更多
                self.statuses += array!
                //加载数据完成 停止动画
                self.indicatorView.stopAnimating()
            } else {
                //首次加载
                self.statuses = array!
            }
            
            //刷新表格
            self.tableView.reloadData()
        }
    }
    //MARK: 懒加载模型数组
    private lazy  var statuses = [Status]()
    
    private lazy var indicatorView: UIActivityIndicatorView = {
        let indicator = UIActivityIndicatorView(activityIndicatorStyle:.Gray)
        return indicator
    }()
    
}

//数据源方法
extension HomeTableViewController {
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statuses.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(HomeCellId, forIndexPath: indexPath) as! StatusCell
        let status = statuses[indexPath.row]
        cell.status = status
        //倒数第二行 加载更多数据  并且小菊花没有转动的时候 再加载更多数据
        if indexPath.row == statuses.count - 1 && !indicatorView.isAnimating(){
            indicatorView.startAnimating()
            loadData()
        }
        return cell
    }
}